package com.looncin.oj.controller;


import com.looncin.oj.annotation.AuthCheck2;
import com.looncin.oj.common.ErrorCode;
import com.looncin.oj.constant.UserConstant;
import com.looncin.oj.exception.BusinessException;
import com.looncin.oj.judge.codesandbox.CodeSandBox;
import com.looncin.oj.judge.codesandbox.CodeSandBoxFactory;
import com.looncin.oj.judge.codesandbox.CodeSandBoxProxy;
import com.looncin.oj.judge.codesandbox.model.ExecuteCodeRequest;
import com.looncin.oj.judge.codesandbox.model.ExecuteCodeResponse;
import com.looncin.oj.model.entity.User;
import com.looncin.oj.service.QuestionService;
import com.looncin.oj.service.QuestionSubmitService;
import com.looncin.oj.service.UserService;
import org.redisson.api.RRateLimiter;
import org.redisson.api.RateIntervalUnit;
import org.redisson.api.RateType;
import org.redisson.api.RedissonClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;


/**
 * 运行用户提交代码测试
 *
 */

@RestController
@RequestMapping("/runCode")
public class sandBoxController {
    @Resource
    private UserService userService;

    @Resource
    private RedissonClient redissonClient;

    @PostMapping
    @AuthCheck2(mustRole = {UserConstant.USER_LOGIN_STATE})
    public ExecuteCodeResponse runCode(@RequestBody ExecuteCodeRequest ECR,HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        RRateLimiter rateLimiter = redissonClient.getRateLimiter(loginUser.getId().toString());
        // 初始化
        // 最大流速 = 每5秒钟产生1个令牌
//        rateLimiter.trySetRate(RateType.OVERALL, 1, 5, RateIntervalUnit.SECONDS);
        rateLimiter.trySetRate(RateType.PER_CLIENT, 1, 5, RateIntervalUnit.SECONDS);
        //需要1个令牌
        if(rateLimiter.tryAcquire(1)){
            //TODO:Do something
            if( ECR==null || ECR.getLanguage()==null ){
                throw new BusinessException(ErrorCode.PARAMS_ERROR);
            }
            if( ECR.getCode() == null ){
                ECR.setCode("");
            }
            if( ECR.getInputList()==null || ECR.getInputList().size() == 0){
                List<String> inputList = Arrays.asList("");
                ECR.setInputList(inputList);
            }

            CodeSandBox codeSandBox = CodeSandBoxFactory.newInstance("remote");
            codeSandBox = new CodeSandBoxProxy(codeSandBox);
            ExecuteCodeResponse executeCodeResponse = codeSandBox.executeCode(ECR);
            return executeCodeResponse;
        }
        throw new BusinessException(ErrorCode.FORBIDDEN_ERROR, "累了休息会吧，请稍后尝试！");
    }

//    public ExecuteCodeResponse runCode(@RequestBody ExecuteCodeRequest ECR,HttpServletRequest request){
//            //TODO:Do something
//            if( ECR==null || ECR.getLanguage()==null ){
//                throw new BusinessException(ErrorCode.PARAMS_ERROR);
//            }
//            if( ECR.getCode() == null ){
//                ECR.setCode("");
//            }
//            if( ECR.getInputList()==null || ECR.getInputList().size() == 0){
//                List<String> inputList = Arrays.asList("");
//                ECR.setInputList(inputList);
//            }
//
//            CodeSandBox codeSandBox = CodeSandBoxFactory.newInstance("remote");
//            codeSandBox = new CodeSandBoxProxy(codeSandBox);
//            ExecuteCodeResponse executeCodeResponse = codeSandBox.executeCode(ECR);
//            return executeCodeResponse;
//    }
}
